util = {}

function parseCookies(cookieString)
        cookieTable = {}
        if not cookieString then return cookieTable end
--        for k, v in string.gmatch(cookieString, "(%p+%a+)=(%w+)") do
        for k, v in string.gmatch(cookieString, "([%p%w]+)=([^;]*)") do
                cookieTable[k] = v
        end
        return cookieTable;
end

function create_guid(create_time, ip)
    --Cat bot 1 ky tu khoi create time, lay toi da 9 ky tu => index
    --chuyen ip tu dang address with dot => ip (in_addr_t) bang ham inet_network cua linux
    if (not create_time) or (not ip) then return nil end
    local index
    if string.len(create_time) >=10 then
        index = 9
    else
        index = string.len(create_time) - 1;
    end
    local ipl = c_inet_network(ip)
    if (ipl==-1) then
        return false
    end
    return string.sub(create_time, 1, index)..ipl;
end

--edited by LuongBTH: 11h15 4/10/2012
function pushCookieHeader(name, value, expires, path, domain, is_secure)
        local guid_cookie_header = name.."="..value
        if expires then
            guid_cookie_header = guid_cookie_header..";expires="..expires
        end
        if path then
                guid_cookie_header = guid_cookie_header..";path="..path
        end
        if domain then
                guid_cookie_header = guid_cookie_header..";domain="..domain
        end

        if is_secure then
                guid_cookie_header = guid_cookie_header..";secure"
        end
        reply.appendHeader(config.SET_COOKIE_HEADER,guid_cookie_header)
end

-- redis_cmd; cmd_format, [optional] cmd_data
util.redis_command = function (biz_name, enable_log, func, cmd_format, cmd_data)
    local con = getRedisConnector(biz_name)
    if con then
        local reply_type ,reply = func(con, cmd_format,cmd_data);
        if not reply_type then
        -- redisContext should not be used again
            redis_connectors[biz_name] = nil
            -- if enable_log then
            --      log
        else
            return reply_type, reply
        end
    end
    -- if enable_log then
    --      log
end

function getRedisConnector(biz_name)
        if redis_connectors[biz_name] then
                return redis_connectors[biz_name];
        else
            return createRedisConnector(biz_name);
        end
end

function createRedisConnector(biz_name)
        local host = ConfigRedisConnectors[biz_name].host;
        local port = ConfigRedisConnectors[biz_name].port;
        local timeout = config.REDIS_CONNECT_TIMEOUT;
        redis_connectors[biz_name] = luredis.new_connector(host, port, timeout);
        if redis_connectors[biz_name] then
                return redis_connectors[biz_name];
        else
                return nil;
        end
end


--by KhanhHH
function getMobileService()
        if mobile_service_ then
                return mobile_service_;
        else
                return createMobileService();
        end
end
function createMobileService()
        local host = config.MOBILE_SERVICE_HOST;
        local port = config.MOBILE_SERVICE_PORT;
        mobile_service_ = mobile_service:new(host,port);
        if mobile_service_ then
                return mobile_service_;
        else
                return nil;
        end
end

--by Luong - 03-09-12
--return "" if can't get osVersion
function parseUserAgent(userAgent)
    local osVersion
    --user agent parse--
    local mbl_service_ = getMobileService()
    if(mbl_service_ ~= nil) then
        osVersion = mbl_service_:parse_user_agent(userAgent)
    end
    if not osVersion then
        osVersion = ""
    end
    return osVersion
end



--by Luong - 26-09
--if key is not exist, return ""
function getStringValue(table, key)
    local value = table[key]
    if value then
            return value
    else
            return ""
    end
end
